#include <iostream>
#include <rapidjson/document.h>
#include <rapidjson/prettywriter.h>
#include <rapidjson/writer.h>
#include <rapidjson/stringbuffer.h>


int main(int argc, char* argv[])
{
    rapidjson::Document json_doc;
    json_doc.SetObject(); // key-value 相当于map，SetValue相当于数组型
    auto& allocator = json_doc.GetAllocator(); // 获取分配器
    
    // 给doc对象赋值
    json_doc.AddMember("name", "张三", allocator);
    //添加数组型数据
    rapidjson::Value array(rapidjson::kArrayType);
    for (int i=0; i<5; i++) {
        rapidjson::Value int_obj(rapidjson::kObjectType);
        int_obj.SetInt(i);
        array.PushBack(int_obj, allocator);
    }

    json_doc.AddMember("number", array, allocator);

    // 将doc对象的值写入字符串
    rapidjson::StringBuffer buffer;  
    // rapidjson::PrettyWriter<rapidjson::StringBuffer> pretty_writer(buffer);  //PrettyWriter是格式化的json，如果是Writer则是换行空格压缩后的json  
    // json_doc.Accept(pretty_writer);
    // std::cout << "pretty_writter: " << std::endl;
    // std::cout << buffer.GetString() << std::endl;
    rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);    
    json_doc.Accept(writer);
    std::cout << "writer: " << std::endl;
    std::cout << buffer.GetString() << std::endl;

    return 0;
} 
